/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.geo.map.scalar;

import java.nio.ByteBuffer;
import technotheist.geo.map.AIntMap;
import technotheist.geo.map.Format;

/**
 *
 * @author Mitchel Strachan
 */
public class UByteMap extends AIntMap<ByteBuffer> {

    static final short UBYTE_MAX = Byte.MAX_VALUE - Byte.MIN_VALUE;
    final ByteBuffer data;

    public UByteMap(int sideLength, boolean normalized) {
        this(sideLength, 1, normalized);
    }

    public UByteMap(int sideLength, int components, boolean normalized) {
        super(sideLength, components, Format.UByte, normalized);
        this.data = ByteBuffer.allocateDirect(byteCount);
    }

    public void set(int p, int u, int v, short value) {
        setInt(p, u, v, value);
    }

    public void set(int p, int u, int v, int component, short value) {
        setInt(index(p, u, v) + component, value);
    }

    @Override
    protected ByteBuffer data() {
        return data;
    }

    @Override
    public int getInt(int bufferIndex) {
        return data.get(bufferIndex);
    }

    @Override
    public void setInt(int bufferIndex, int value) {
        if (value < 0 || value > UBYTE_MAX) {
            throw new IllegalArgumentException("value must be between " + 0 + " and " + UBYTE_MAX);
        }
        data.put(bufferIndex, (byte) value);
    }
}
